package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.BankDataBean;
import com.meihua.baoleyi.bean.BankInfo;
import com.meihua.baoleyi.bean.BaseBean;
import com.meihua.baoleyi.bean.WithdrawalsBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.BankPresenter;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.StringUtils;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.view.IBankView;

import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;
import okhttp3.Response;


/**
 * 账户提现界面
 */
public class AccountCashActivity extends BaseActivity implements View.OnClickListener, IBankView {
    private TextView tv_center;
    private RelativeLayout rl_iv_back;
    private Button btn_tixian;//确认
    private TextView tv_money, tv_alltixian;//可用余额  全部提现
    private EditText et_money;
    private RelativeLayout rl_bank;//卡
    private ImageView text_back;//返回
    private TextView text_title;//标题
    private TextView tv_card_end_num;//银行卡号
    private ImageView iv_head;
    private TextView tv_bankname;//银行名称

    private BankInfo mCrruntBank;
    private BankPresenter mPresenter;
    private final int CODE_STARTBANK = 001;
    private static final int REQUEST_CHOOSE_BANK_CARD = 0x76;
    private BankDataBean.DataBean bankBean;
    private int crrunt = 0;
    private String yu_e;
    private TextView tv_choose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhang_huti_xian);
        yu_e = getIntent().getStringExtra("yu_e");
        initView();
    }

    /**
     * 初始化布局View
     */
    private void initView() {
        text_back = (ImageView) findViewById(R.id.text_back);
        text_title = (TextView) findViewById(R.id.text_title);
        text_back.setOnClickListener(this);
        text_title.setText("提现");

        btn_tixian = (Button) findViewById(R.id.btn_tixian);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_alltixian = (TextView) findViewById(R.id.tv_alltixian);
        et_money = (EditText) findViewById(R.id.et_money);
        rl_bank = (RelativeLayout) findViewById(R.id.rl_bank);
        tv_card_end_num = (TextView) findViewById(R.id.tv_card_end_num);
        iv_head = (ImageView) findViewById(R.id.iv_head);
        tv_bankname = (TextView) findViewById(R.id.tv_bankname);
        tv_choose = (TextView) findViewById(R.id.tv_choose);
        rl_bank.setOnClickListener(this);
        btn_tixian.setOnClickListener(this);
        tv_alltixian.setOnClickListener(this);
        tv_money.setText("可用余额" + yu_e + "元");
        mPresenter = new BankPresenter(this, (IBankView) this);
    }

    /*界面返回的数据*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 11) finish();
        if (requestCode == CODE_STARTBANK && data != null) {
            bankBean = (BankDataBean.DataBean) data.getSerializableExtra("bankCard");
            tv_choose.setVisibility(View.INVISIBLE);
            if (!StringUtils.bankLast(bankBean.getBank_card()).isEmpty()) {
                tv_card_end_num.setText("尾号"+ StringUtils.bankLast(bankBean.getBank_card()) +"银行卡"); //银行卡号
            }
            tv_bankname.setText(bankBean.getBanks_name()); //银行名称
            if (!bankBean.getBanks_img().isEmpty()) {
                Glide.with(this).load(bankBean.getBanks_img()).error(R.mipmap.chanpin).into(iv_head); //银行图标
            }
        }
    }

    /*获取服务器数据*/
    public void getServerData() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());
        treeMap.put("money", et_money.getText().toString().trim());
        treeMap.put("account_bank", bankBean.getBank_card());//银行卡号
        treeMap.put("bank_id", bankBean.getBanks_id() + "");//银行id
        NetHelper.post(APIServer.WITHDRAWALS, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                WithdrawalsBean bean = NetHelper.getBeanForJson(WithdrawalsBean.class, s);
                if (bean == null) return;
                if (bean.getCode().equals("200")) {
                    dismissDialog();
                    Toast.makeText(AccountCashActivity.this, "提现成功", Toast.LENGTH_SHORT).show();
                    Intent in = new Intent();
                    in.putExtra("finish", 1);
                    setResult(1, in);
                    finish();
                } else {
                    dismissDialog();
                    ToastUtil.show(AccountCashActivity.this, bean.getMsg());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_back:
                finish();
                break;
            case R.id.tv_alltixian://全部提现
                et_money.setText(yu_e);
                break;
            case R.id.btn_tixian://确认提现
                if (TextUtils.isEmpty(et_money.getText().toString().trim()) || bankBean == null) {
                    Toast.makeText(this,"请填写完整信息",Toast.LENGTH_SHORT).show();
                    return;
                }
                getServerData();
                break;
            case R.id.rl_bank://选择卡
                /*跳到银行卡列表界面*/
                startActivityForResult(new Intent(this, BankActivity.class).putExtra("type", 2), CODE_STARTBANK);
                break;
        }
    }

    /**
     * 展示银行卡
     *
     * @param list
     */
    @Override
    public void showBank(List<BankInfo> list) {
        if (list != null && list.size() > crrunt) {
            mCrruntBank = list.get(crrunt);
            if (mCrruntBank != null) {
                Glide.with(this).load(mCrruntBank.getImg()).placeholder(R.drawable.ic_launcher).error(R.drawable.ic_launcher).into(iv_head);
                tv_bankname.setText(mCrruntBank.getBanks_name() + "");
                String s = mCrruntBank.getBanks_card();
                if (s.length() >= 4) {
                    s = s.substring(s.length() - 4);
                    tv_card_end_num.setVisibility(View.VISIBLE);
                    tv_card_end_num.setText("尾号" + s + "储蓄卡");
                }
            }
        }
    }

    /**
     * 提现回调
     *
     * @param b
     */
    @Override
    public void onCash(BaseBean b) {
        if (b != null) {
            ToastUtil.show(this, b.getMsg());
            if (b.getCode().equals("1")) {
                finish();
            }
        }
    }
}
